package com.youlu.campus.service.pay.impl;

import com.youlu.campus.entity.UserItemCategoryPayRecord;
import com.youlu.campus.service.pay.UserItemCategoryPayRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UserItemCategoryPayRecordServiceImpl implements UserItemCategoryPayRecordService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public UserItemCategoryPayRecord create(UserItemCategoryPayRecord req) {
        req.setId(null);
        req.setCreatedTime(new Date());
        return mongoTemplate.insert(req);
    }

    @Override
    public boolean update(UserItemCategoryPayRecord req) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(req.getId()));
        Update update = new Update();
        update.set("categoryId", req.getCategoryId());
        update.set("comment", req.getComment());
        update.set("status", req.getStatus());
        update.set("payTime", req.getPayTime());
        update.set("updatedTime", new Date());
        return mongoTemplate.updateFirst(query, update, UserItemCategoryPayRecord.class).getModifiedCount() > 0 ? true : false;
    }

    @Override
    public List<UserItemCategoryPayRecord> findByUserId(String userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        return mongoTemplate.find(query, UserItemCategoryPayRecord.class);
    }
}
